// 本地存储工具类
class Storage {
  constructor(storage = localStorage) {
    this.storage = storage
  }

  // 设置缓存
  set(key, value, duration = 24 * 60 * 60 * 1000) { // 默认24小时
    const item = {
      value,
      timestamp: Date.now(),
      duration
    }
    this.storage.setItem(key, JSON.stringify(item))
  }

  // 获取缓存
  get(key) {
    const item = this.storage.getItem(key)
    if (!item) return null

    const { value, timestamp, duration } = JSON.parse(item)
    const isExpired = Date.now() - timestamp > duration

    if (isExpired) {
      this.storage.removeItem(key)
      return null
    }

    return value
  }

  // 移除缓存
  remove(key) {
    this.storage.removeItem(key)
  }

  // 清空缓存
  clear() {
    this.storage.clear()
  }
}

// 创建实例
export const localStorage = new Storage(window.localStorage)
export const sessionStorage = new Storage(window.sessionStorage) 